package com.jmdz.fmis.admin.api.memoria;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jmdz.fmis.core.exception.AdminServiceException;
import com.jmdz.fmis.core.exception.ExceptionDefinition;
import com.jmdz.fmis.core.exception.ServiceException;
import com.jmdz.fmis.data.model.Page;
import com.jmdz.fmis.data.domain.MemorialInfoDO;
import com.jmdz.fmis.data.mapper.MemorialInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.session.RowBounds;

import java.util.Date;
import java.util.List;

/**
 * Generate Code By Fmis
 */
@Service
public class MemoriaServiceImpl implements MemoriaService {

    @Autowired
    private MemorialInfoMapper memorialInfoMapper;

    @Override
    public boolean delete(Long id, Long adminId) throws ServiceException {
        return memorialInfoMapper.deleteById(id) > 0;
    }

    @Override
    public Page<MemorialInfoDO> list(String name, Integer page, Integer limit, Long adminId) throws ServiceException {
        QueryWrapper<MemorialInfoDO> wrapper = new QueryWrapper<MemorialInfoDO>();
        if (name != null) {
            wrapper.eq("name", name);
        }
        return memorialInfoMapper.selectPage(Page.div(page,limit,MemorialInfoDO.class), wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MemorialInfoDO create(Long adminId) throws ServiceException {
        Date now = new Date();
        MemorialInfoDO insertDO = new MemorialInfoDO();
        insertDO.setGmtUpdate(now);
        insertDO.setGmtCreate(now);
        if (memorialInfoMapper.insert(insertDO) > 0) {
            return insertDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(Long id, Long adminId) throws ServiceException {
        Date now = new Date();
        MemorialInfoDO updateDO = new MemorialInfoDO();
        updateDO.setId(id);
        updateDO.setGmtUpdate(now);
        if (memorialInfoMapper.updateById(updateDO) > 0) {
            return "ok";
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

}
